﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ExamSharedClasses {
    /// <summary>
    /// (k) Der Antworttyp, der mehrere Auswahlmöglichkeiten zuläßt, und die Antwort
    /// bitweise in einer Integer-Variablen hält.
    /// </summary>
    [Serializable()]
    public class AnswerTypeMultiple : Answer {
        new public const string TABLENAME = "answer_type_multiple";
        public const string ANSWER_USER_ANSWER = "answer_user_answer";

        public int AnswerUserAnswer { get; set; }

        public AnswerTypeMultiple()
            : base() {
            this.AnswerUserAnswer = -1;
        }
        public AnswerTypeMultiple( Answer answer )
            : base( answer ) {
            this.AnswerUserAnswer = -1;
        }
        public AnswerTypeMultiple( int examId ) : this() {
            AnswerExamId = examId;
        }
        public AnswerTypeMultiple( int examId , Question question) : this() {
            AnswerExamId = examId;
            AnswerQuestion = question;
        }

        public override string ToMoreDetailedString() {
            StringBuilder sb = new StringBuilder( base.ToMoreDetailedString() + "AnswerTypeMultiple: " );
            sb.Append( "ANSWER: " + AnswerUserAnswer + '\n' );
            return sb.ToString();
        }

        /// <summary>
        /// (k) es wird sicherlich bei der Antwort nur dieser Text benötigt
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return "angekreuzt: " + Tools.EnumerateLeftToRightPositionsOfPowersOfTwo( AnswerUserAnswer ) + '\n';
        }
        public override int GetAchievedPoints( int maxPointsInt ) {
            double maxPoints = maxPointsInt, achievedPoints = 0.0;
            QuestionTypeMultiple qtm = ( QuestionTypeMultiple )AnswerQuestion;
            double teiler = 4; // hängt von Anzahl der möglichen Antworten ab...
            if ( qtm.QuestionAnswer4 == null || qtm.QuestionAnswer4.Equals( "" ) )
                teiler = 3;
            if ( qtm.QuestionAnswer3 == null || qtm.QuestionAnswer3.Equals( "" ) )
                teiler = 2;
            for ( int k = 0; k < teiler; k++ ) {
                // kommt das Bit in der richtigen Antwort vor... und auch in der User-Antwort
                if ( ( ( ( qtm.QuestionRightAnswer & ( 1 << k ) ) == ( 1 << k ) ) &&
                     ( ( AnswerUserAnswer & ( 1 << k ) ) == ( 1 << k ) ) ) || (
                    // ...oder in beiden nicht
                        ( ( qtm.QuestionRightAnswer & ( 1 << k ) ) == 0 ) &&
                        ( ( AnswerUserAnswer & ( 1 << k ) ) == 0 ) )
                    ) {
                    // dann einen Anteil der 
                    achievedPoints += ( maxPoints / teiler );
                }

            }
            return ( int )achievedPoints;
        }

        public override bool HasSetAnswer() {
            return (AnswerUserAnswer!=-1);
        }
        public override void SetDefaultAnswer() {
            AnswerUserAnswer = 0;
        }
        public override bool HasEmptyAnswer() {
            return !HasSetAnswer();
        }

    }
}
